package pl.mcnova.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import pl.mcnova.model.HibernateSession;
import pl.mcnova.model.entity.PhoneNumber;

public class PhoneNumberDAO {

	private SessionFactory sessionFactory;
	private Session session;

	public PhoneNumberDAO() {

		sessionFactory = HibernateSession.getInstance();

	}

	public void save(PhoneNumber phoneNumber) {
		session = sessionFactory.openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.save(phoneNumber);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				try {
					transaction.rollback();

				} catch (HibernateException he) {

				}
			}
		} finally {
			try {
				session.close();
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public void update(PhoneNumber phoneNumber) {
		session = sessionFactory.openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.update(phoneNumber);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				try {
					transaction.rollback();

				} catch (HibernateException he) {

				}
			}
		} finally {
			try {
				session.close();
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public void delete(PhoneNumber phoneNumber) {
		session = sessionFactory.openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.delete(phoneNumber);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				try {
					transaction.rollback();

				} catch (HibernateException he) {

				}
			}
		} finally {
			try {
				session.close();
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	@SuppressWarnings("finally")
	public PhoneNumber get(Integer phoneNumberID) {
		session = sessionFactory.openSession();
		PhoneNumber phoneNumber = null;

		try {

			phoneNumber = (PhoneNumber) session.get(PhoneNumber.class, phoneNumberID);

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return phoneNumber;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	@SuppressWarnings("finally")
	public List<PhoneNumber> get(String number) {
		session = sessionFactory.openSession();
		List<PhoneNumber> phones = null;

		try {

			Criteria criteria = session.createCriteria(PhoneNumber.class);
			criteria.add(Restrictions.like("number", number));
			phones = criteria.list();

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return phones;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public List<String> getAllNumbersfrom(Integer id) {
		session = sessionFactory.openSession();

		List<String> phoneNumbers = null;

		try {

			// Query q =
			// session.createQuery("from phone_numbers where contractor_id = "+id.toString());
			Query q = session.createSQLQuery("select number from phone_numbers where contractor_id = " + id.toString());
			phoneNumbers = q.list();

			// Criteria criteria = session.createCriteria(PhoneNumber.class);
			// Property c_id = Property.forName("contractor_id");
			// criteria.add(Restrictions.disjunction().add(c_id.eq(id)));
			// phoneNumbers = criteria.list();
			//

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return phoneNumbers;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public PhoneNumber getFirst(Integer contractorId) {
		session = sessionFactory.openSession();

		try {

			Query q = session.createSQLQuery("select * from phone_numbers p where p.contractor_id = "
					+ contractorId.toString() + " limit 1");
			if (q.list() != null) return (PhoneNumber) q.list().get(0);
			return null;

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return null;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}
}
